package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseFragment
import com.lancoo.znbkxx.teacher.model.data.Vocab
import com.lancoo.znbkxx.teacher.net.BkApiMethods
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_unstudy_vocab.*
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeoutException

/**
 *@author Admin
 *@date 2020/12/25
 *
 */
class UnStudyVocabListFragment : BaseFragment() {
    private var totalSize = 0
    private var page = 1
    private val zsd by lazy { arguments?.getString("zsd") }
    private val type by lazy { arguments?.getInt("type") }
    private val stuID by lazy { arguments?.getString("stuID") }
    private val vocabList = mutableListOf<Vocab>()
    private lateinit var adapter: UnStudyVocabListAdapter
    override fun createLayout(): Int {
        return R.layout.fragment_unstudy_vocab
    }

    override fun initView(view: View?) {
        adapter = UnStudyVocabListAdapter(type!!, vocabList)
        rv_vocab.layoutManager = LinearLayoutManager(requireContext())
        rv_vocab.adapter = adapter
        swipe_layout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                vocabList.clear()
                getStuUnStudyVocabInfoList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (vocabList.size <= totalSize) {
                    page += 1
                }
                getStuUnStudyVocabInfoList()
            }
        })
        getStuUnStudyVocabInfoList()
    }

    @SuppressLint("CheckResult")
    fun getStuUnStudyVocabInfoList() {
        val method = BkApiMethods(requireContext())
        val params = BkApiMethods.Paramas()
        params["ZsdArea"] = zsd
        params["VocabType"] = type
        params["Page"] = page
        params["StuID"]=stuID
        method.getStuUnStudyVocabInfoList(Ext.RZZD_URL, params).subscribe(
                {
                    if (it.Code == 1) {
                        tv_total.setTextColor(Color.parseColor(when (type) {
                            0 -> "#00afff"
                            1 -> "#22d7bb"
                            else -> "#999999"
                        }))
                        tv_total.text = when (type) {
                            0 -> "常考词汇(${it.Data.FirstVocab}个)"
                            1 -> "次常考词汇(${it.Data.SecondVocab}个)"
                            else -> "不常考词汇(${it.Data.ThirdVocab}个)"
                        }
                        totalSize = when (type) {
                            0 -> it.Data.FirstVocab
                            1 -> it.Data.SecondVocab
                            else -> it.Data.ThirdVocab
                        }
                        vocabList.addAll(when (type) {
                            0 -> it.Data.FirstVocabList
                            1 -> it.Data.SecondVocabList
                            else -> it.Data.ThirdVocabList
                        })
                        adapter.setNewData(vocabList)
                        swipe_layout.finishRefresh()
                        swipe_layout.setEnableLoadMore(vocabList.size < totalSize)
                        if (vocabList.size >= totalSize) {
                            swipe_layout.finishLoadMoreWithNoMoreData()
                        } else {
                            swipe_layout.finishLoadMore()
                        }
                    } else {
                        MyToast.showToast(it.Message)
                    }
                }, {
            val error: String = if (it is TimeoutException || it is SocketTimeoutException
                    || it is ConnectException
            ) {
                getString(R.string.aibk_network_timeout)
            } else {
                getString(R.string.aibk_server_sick)
            }
            MyToast.showToast(error)
        })
    }
}